/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.jrwz.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.jrwz.configure.Config;
import l1j.jrwz.L1DatabaseFactory;
import l1j.jrwz.server.model.L1Spawn;
import l1j.jrwz.server.model.Instance.L1PcInstance;
import l1j.jrwz.server.templates.L1Npc;
import l1j.jrwz.server.utils.SQLUtil;

// Referenced classes of package l1j.jrwz.server:
// MobTable, IdFactory

public class NpcSpawnTable {

    private static Logger _log = Logger
            .getLogger(NpcSpawnTable.class.getName());

    private static NpcSpawnTable _instance;

    public static NpcSpawnTable getInstance() {
        if (_instance == null) {
            _instance = new NpcSpawnTable();
        }
        return _instance;
    }

    private final Map<Integer, L1Spawn> _spawntable = new HashMap<Integer, L1Spawn>();

    private int _highestId;

    private NpcSpawnTable() {
        fillNpcSpawnTable();
    }

    public void addNewSpawn(L1Spawn l1spawn) {
        _highestId++;
        l1spawn.setId(_highestId);
        _spawntable.put(l1spawn.getId(), l1spawn);
    }

    private void fillNpcSpawnTable() {

        int spawnCount = 0;

        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {

            con = L1DatabaseFactory.getInstance().getConnection();
            pstm = con.prepareStatement("SELECT * FROM spawnlist_npc");
            rs = pstm.executeQuery();
            while (rs.next()) {
                if (!Config.ALT_GMSHOP) { // GM商店
                    int npcid = rs.getInt("id");
                    if (npcid >= Config.ALT_GMSHOP_MIN_ID
                            && npcid <= Config.ALT_GMSHOP_MAX_ID) {
                        continue;
                    }
                }
                if (!Config.ALT_TALKINGSCROLLQUEST) { // 说话卷轴任务
                    int npcid = rs.getInt("id");
                    if (npcid >= 544 && npcid <= 558) {
                        continue;
                    }
                }
                int npcTemplateid = rs.getInt("npc_templateid");
                L1Npc l1npc = NpcTable.getInstance().getTemplate(npcTemplateid);
                L1Spawn l1spawn;
                if (l1npc == null) {
                    _log.warning("mob data for id:" + npcTemplateid
                            + " missing in npc table");
                    l1spawn = null;
                } else {
                    if (rs.getInt("count") == 0) {
                        continue;
                    }
                    l1spawn = new L1Spawn(l1npc);
                    l1spawn.setId(rs.getInt("id"));
                    l1spawn.setAmount(rs.getInt("count"));
                    l1spawn.setLocX(rs.getInt("locx"));
                    l1spawn.setLocY(rs.getInt("locy"));
                    l1spawn.setRandomx(rs.getInt("randomx"));
                    l1spawn.setRandomy(rs.getInt("randomy"));
                    l1spawn.setLocX1(0);
                    l1spawn.setLocY1(0);
                    l1spawn.setLocX2(0);
                    l1spawn.setLocY2(0);
                    l1spawn.setHeading(rs.getInt("heading"));
                    l1spawn.setMinRespawnDelay(rs.getInt("respawn_delay"));
                    l1spawn.setMapId(rs.getShort("mapid"));
                    l1spawn.setMovementDistance(rs.getInt("movement_distance"));
                    l1spawn.setName(l1npc.get_name());
                    l1spawn.init();
                    spawnCount += l1spawn.getAmount();

                    _spawntable.put(new Integer(l1spawn.getId()), l1spawn);
                    if (l1spawn.getId() > _highestId) {
                        _highestId = l1spawn.getId();
                    }
                }
            }
        } catch (SQLException e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            SQLUtil.close(rs);
            SQLUtil.close(pstm);
            SQLUtil.close(con);
        }

        _log.config("加载NPC配置资料表 " + _spawntable.size() + " 个");
        _log.fine("NPC总数 " + spawnCount + " 个");
    }

    public L1Spawn getTemplate(int i) {
        return _spawntable.get(i);
    }

    public void storeSpawn(L1PcInstance pc, L1Npc npc) {
        Connection con = null;
        PreparedStatement pstm = null;

        try {
            int count = 1;
            String note = npc.get_name();

            con = L1DatabaseFactory.getInstance().getConnection();
            pstm = con
                    .prepareStatement("INSERT INTO spawnlist_npc SET location=?,count=?,npc_templateid=?,locx=?,locy=?,heading=?,mapid=?");
            pstm.setString(1, note);
            pstm.setInt(2, count);
            pstm.setInt(3, npc.get_npcId());
            pstm.setInt(4, pc.getX());
            pstm.setInt(5, pc.getY());
            pstm.setInt(6, pc.getHeading());
            pstm.setInt(7, pc.getMapId());
            pstm.execute();
        } catch (Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);

        } finally {
            SQLUtil.close(pstm);
            SQLUtil.close(con);
        }
    }

}
